package cn.edu.lingnan.controllerV1;


 import cn.edu.lingnan.service.ClassService;
 import cn.edu.lingnan.user.Statistics.StudentStatsVo;
 import cn.edu.lingnan.user.dtos.ClassQueryDTO;
import cn.edu.lingnan.user.dtos.ClassStudentQueryDTO;
import cn.edu.lingnan.user.dtos.Result;
import cn.edu.lingnan.user.pojos.Class;
import cn.edu.lingnan.user.pojos.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
@Slf4j
public class ClassController {

    @Autowired
    private ClassService classService;

    //分页查询
    @PostMapping("/list")
    public Result<PageBean> getClassList(@RequestBody ClassQueryDTO queryDTO){
        log.info("查询班级列表：{}", queryDTO);
        PageBean result = classService.getClassList(queryDTO);
        return Result.success(result);
    }

    //根据班级id查询班级信息
    @GetMapping("/classInfo")
    public Result<Class> getClassInfo(@RequestParam("id") Integer id){
        log.info("根据班级id查询班级信息：{}", id);
        Class classInfo = classService.getClassInfo(id);
        if (classInfo != null){
            return Result.success(classInfo);
        }
        return Result.error("班级不存在");
    }

    //添加班级
    @PostMapping("/add")
    public Result addClass(@RequestBody @Validated Class classInfo){
        log.info("添加班级：{}", classInfo);
        classService.addClass(classInfo);
        return Result.success("添加班级成功");
    }

    //修改班级信息
    @PostMapping("/update")
    public Result updateClass(@RequestBody @Validated Class classInfo){
        log.info("修改班级信息：{}", classInfo);
        classService.updateClass(classInfo);
        return Result.success("修改班级信息成功");
    }

    //删除班级
    @DeleteMapping("/delete")
    public Result deleteClass(@RequestParam("id") Integer id){
        //查询该班级是否有学生，如果有学生，则不能删除
        List<Integer> list = classService.getStudentIdListByClassId(id);
        if (!list.isEmpty()){
            System.out.println("该班级还有学生（studentId）："+list);
            return Result.error("请先移除班级内所有学生");
        }
        //删除班级,服务层做事务回滚
        boolean flag = classService.deleteClassById(id);
        if (!flag) {
            return  Result.error("删除班级失败");
        }
        return Result.success("删除班级成功") ;
    }

    //分页查询班级下的学生
    @PostMapping("/studentList")
    public Result<PageBean> getStudentList(@RequestBody(required = false) ClassStudentQueryDTO queryDTO,@RequestParam Integer id){
        log.info("查询班级学生列表：{}", queryDTO);
        PageBean result = classService.getStudentList(queryDTO,id);
        return Result.success(result);
    }

    //查询学生统计信息
    @GetMapping("/studentStats")
    public Result<StudentStatsVo> getStudentStats(@RequestParam Integer id){
        log.info("查询班级学生统计信息：{}", id);
        StudentStatsVo result = classService.getStudentStats(id);
        return Result.success(result);
    }

    //移除学生出班级
    @DeleteMapping("/removeStudent")
    public Result removeStudent(@RequestParam("studentId") Integer studentId,@RequestParam("classId") Integer classId){
        log.info("移除学生出班级：{}", studentId);
        boolean flag = classService.removeStudent(studentId,classId);
        if (!flag) {
            return  Result.error("移除学生出班级失败");
        }
        return Result.success("移除学生出班级成功") ;
    }


}
